#include<iostream>
#include<string.h>
using std::cout;
using std::endl;
class Student{
public:
    //构造函数
    Student(int id,int age,const char*name)
    :_stuID(id)
    ,_stuAge(age)
    ,_stuName(new char[strlen(name)+1]())
    {
        strcpy(_stuName,name);
    }
    //打印
    void print(){
        printf("stu_id = %d stu_Age = %d stuName = %s\n",_stuID,_stuAge,_stuName);
    }
    //拷贝构造函数
    Student(const Student &rhs)
    :_stuID(rhs._stuID)
    ,_stuAge(rhs._stuAge)
    ,_stuName(new char[strlen(rhs._stuName)+1]())
    {
        //深拷贝
        strcpy(_stuName,rhs._stuName);
        cout<<"this is copy"<<endl;
    }

    //赋值运算符函数
    Student& operator =(const Student &rhs){
        if(this != &rhs) //比较地址
        {
            delete [] this->_stuName;
            this->_stuName = new char[strlen(rhs._stuName)+1]();
            strcpy(this->_stuName,rhs._stuName);
            this->_stuID = rhs._stuID;
            this->_stuAge = rhs._stuAge;
        }
        cout<<"this is operator"<<endl;
        return *this;
    }

    //析构函数
    ~Student(){
        //要判断是不是空
        if(!_stuName){
            delete [] _stuName;
            _stuName = nullptr;
        }
        cout<<"this is ~Student"<<endl;
    }

    //new delete
    void*operator new(size_t sz){
        cout<<"void *operator new(size_t sz)"<<endl;
        return malloc(sz);
    }
    void operator delete(void*p){
        cout<<"operator delete (void*)"<<endl;
        free(p);
    }
private:
    int _stuID;
    int _stuAge;
    char *_stuName;
};


int main(void){
    //调用构造函数
    Student stu1(001,18,"wtto");
    stu1.print();
    Student stu2 = stu1; //拷贝构造函数
    stu2.print();
    stu2 = Student(003,21,"lisi"); //临时变量 且对象已经存在，调用赋值运算符
    stu2.print();
    
    return 0;   
}